package com.starleague.hexagonal.android;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starleague.battletech.android.BattletechActivity;
import com.starleague.battletech.hexagonal.IBattletechHex;
import com.starleague.battletech.hexagonal.IBattletechHexTerrain;
import com.starleague.hexagonal.IHex;

public class HexInfoView
        extends LinearLayout
{
    private static final Logger log = LoggerFactory.getLogger(HexInfoView.class);

    private final TextView coordTextView, elevationTextView;
    private final LinearLayout.LayoutParams layoutParams;
    private final List<TextView> terrainTextViewList = new ArrayList<TextView>();

    // private EventBus eventBus;
    private final BattletechActivity battletechActivity;

    // private IHex selectedHex = null;

    public HexInfoView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public HexInfoView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        log.info("HexInfoView.ctor(): {}", context);

        // if (context instanceof HasEventBus)
        // {
        // this.eventBus = ((HasEventBus) context).getEventBus();
        // }
        // else
        // {
        // throw new RuntimeException("HexInfoView.initView(): context is not a [HasEventBus]");
        // }

        if (context instanceof BattletechActivity)
        {
            this.battletechActivity = (BattletechActivity) context;
        }
        else
        {
            throw new RuntimeException("HexInfoView.initView(): context is not a [BattletechActivity]");
        }

        this.layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                                          ViewGroup.LayoutParams.WRAP_CONTENT);

        this.coordTextView = createTextView(context);
        this.addView(this.coordTextView, this.layoutParams);

        this.elevationTextView = createTextView(context);
        this.addView(this.elevationTextView, this.layoutParams);
    }

    private TextView createTextView(Context context)
    {
        TextView textView = new TextView(context);
        textView.setBackgroundColor(Color.LTGRAY);
        textView.setTextColor(Color.BLACK);
        textView.setPadding(20, 1, 10, 1);
        textView.setTextSize(12);
        return textView;
    }

    // @Override
    // protected void onAttachedToWindow()
    // {
    // log.info("HexInfoView.onAttachedToWindow()");
    //
    // super.onAttachedToWindow();
    //
    // this.eventBus.register(this);
    // }
    //
    // @Override
    // protected void onDetachedFromWindow()
    // {
    // log.info("HexInfoView.onDetachedFromWindow()");
    // this.eventBus.unregister(this);
    //
    // super.onDetachedFromWindow();
    // }

    //
    // Handle Events
    //

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent)
    {
        log.info("HexInfoView.onTouchEvent(): {}", motionEvent);

        this.setVisibility(INVISIBLE);

        return true; // super.onTouchEvent(motionEvent);
    }

    // @Subscribe
    // public void handleHexSelectedEvent(HexSelectedEvent hexEvent)
    // {
    // log.info("HexInfoView.handleHexSelectedEvent(): {}", hexEvent);
    //
    // // if (this.hexInfoPopup.isShowing())
    // // {
    // // this.hexInfoPopup.dismiss();
    // // }
    //
    // this.selectedHex = hexEvent.getHex();
    //
    // // int localX = 0;
    // // int localY = 0;
    // //
    // // if (this.selectedHex != null)
    // // {
    // // IHexCoordinate selectedHexCoordinate = this.selectedHex.getHexCoordinate();
    // //
    // // // TODO: There must be an easier way to calculate the popup position
    // // // TODO: This is wrong, if there is a HexDetailView :[
    // //
    // // localX = this.getHexGridMetrics().getHexLeft(selectedHexCoordinate)
    // // - this.getScrollX()
    // // + (this.getHexGridMetrics().getHexWidth() / 2);
    // //
    // // localY = this.getHexGridMetrics().getHexTop(selectedHexCoordinate)
    // // - this.getScrollY()
    // // + (this.getHexGridMetrics().getHexHeight() / 2);
    // //
    // // }
    // //
    // // log.warn("HexGridView.handleHexSelectedEvent: Local [{} {}]: {}",
    // // localX,
    // // localY,
    // // this.selectedHex);
    //
    // setHexAt(this.selectedHex, 0, 0);
    //
    // this.invalidate();
    // }

    //
    // @Subscribe
    // public void handleHexSelectedEvent(HexSelectedEvent hexEvent)
    // {
    public void setHexAt(IHex hex,
                         int parentX,
                         int parentY)
    {
        log.warn("HexInfoView.setHexAt [{} {}]: {}", parentX, parentY, hex);

        IBattletechHex battletechHex = (IBattletechHex) hex;

        for (TextView terrainTextView : this.terrainTextViewList)
            this.removeView(terrainTextView);
        this.terrainTextViewList.clear();

        if ((battletechHex != null) && (this.getParent() instanceof FrameLayout))
        {
            this.coordTextView.setText("COORD: " + battletechHex.getHexCoordinate());
            this.elevationTextView.setText("ELEVATION: " + battletechHex.getElevation());

            for (IBattletechHexTerrain hexTerrain : battletechHex.getHexTerrainList())
            {
                TextView terrainTextView = createTextView(this.battletechActivity);

                terrainTextView.setText("TERRAIN: "
                                        + hexTerrain.getHexTerrainType()
                                        + " ("
                                        + hexTerrain.getLevel()
                                        + ")");

                this.addView(terrainTextView, this.layoutParams);
                this.terrainTextViewList.add(terrainTextView);
            }

            //

            FrameLayout parentFrameLayout = (FrameLayout) getParent();

            // HexGridView hexGridView = (HexGridView) ((View) this.getParent())
            // .findViewById(R.id.hex_grid_view);

            int gravity = Gravity.NO_GRAVITY;
            gravity |= (parentX < (parentFrameLayout.getWidth() / 2))
                    ? Gravity.RIGHT
                    : Gravity.LEFT;
            gravity |= (parentY < (parentFrameLayout.getHeight() / 2))
                    ? Gravity.BOTTOM
                    : Gravity.TOP;

            ((FrameLayout.LayoutParams) this.getLayoutParams()).gravity = gravity;

            this.setVisibility(VISIBLE);
        }
        else
        {
            this.coordTextView.setText("COORD: NONE");
            this.elevationTextView.setText("ELEVATION: NONE");
            this.setVisibility(INVISIBLE);
        }

        this.invalidate();
    }
}
